package com.example.ui.share;

import java.util.ArrayList;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.StartFragment1;
import com.example.ui.learn.util.CurriculumCenter;
import com.example.ui.learn.util.TypeAdapter;
import com.example.ui.register.R5Fragment;
import com.example.ui.register.RegisterActivity05;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

@SuppressLint("HandlerLeak")
public class IndustryFragment extends Fragment {
	private ListView tf_lv;
	private String data;
	private ArrayList<CurriculumCenter> item1_cc= new ArrayList<CurriculumCenter>();
	private TypeAdapter item_adapter;
	
	public static int type;// 1register5 2startactivity 3createcircle
	 private Button btnleft, btnright;
	private TextView tvtitle;
	private RelativeLayout rl_top;
	AlertDialog infoad;
	AlertDialog.Builder bulider;
	public static String ind="";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View v = inflater.inflate(R.layout.a_tf, null);				
		tf_lv = (ListView) v.findViewById(R.id.at_lv);
		rl_top=(RelativeLayout) v.findViewById(R.id.ai_top);
		bulider = new AlertDialog.Builder(getActivity());
		bulider.setView(View.inflate(getActivity(), R.layout.onloading, null))
				.create();
		infoad = bulider.show();
		new Thread(item1_run).start();		
		switch (type) {
		
		case 2:
			rl_top.setVisibility(View.VISIBLE);
			init(v);
			break;
	

		default:
			break;
		}
		item_adapter = new TypeAdapter(v.getContext(), item1_cc);
		tf_lv.setAdapter(item_adapter);
		tf_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch (type) {
				case 1:
					ind = item1_cc.get(arg2).getTitle();		
					RegisterActivity05.h.sendEmptyMessage(1);
					break;
				case 2:
					StartFragment1.type=item1_cc.get(arg2).getTitle();
					StartFragment1.typeId=item1_cc.get(arg2).getId();
					break;
				case 3:
					CreateCircleGroupActivity.circleIndustry = item1_cc.get(arg2).getTitle();
					CreateCircleGroupActivity.circleIndustryid = item1_cc.get(arg2).getId();
					
					break;

				default:
					break;
				}
				getActivity().onBackPressed();
			}
		});
		return v;
	}
    private void init(View v) {
			// top
			btnleft = (Button) v.findViewById(R.id.layout_title_left_btn);
			btnright = (Button) v.findViewById(R.id.layout_title_right_btn);
			tvtitle = (TextView) v.findViewById(R.id.layout_title_middle_text);
			btnleft.setText("返回");
			tvtitle.setText("发起活动");
			btnright.setText("提交");
			btnright.setOnClickListener(type_ocl);
			btnleft.setOnClickListener(type_ocl);
		}
    OnClickListener type_ocl=new OnClickListener() {
		
  		@Override
  		public void onClick(View v) {
  			switch (v.getId()) {
  			case R.id.layout_title_right_btn:
  				getActivity().onBackPressed();

  				break;
  			case R.id.layout_title_left_btn:
  				getActivity().onBackPressed();
  				break;
  			}
  			
  			
  		}
  	};
	Runnable item1_run = new Runnable() {

		@Override
		public void run() {
			switch (type) {
			case 1:
				data = NetRequest.Request_register4ind();				 
				break;
			default:
				data=NetRequest.Request_Item01();
				break;
			}
			
			item1_cc = AnalyzingJson.getdata_item1(data);
			h.sendEmptyMessage(1);
		}
	};
	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			item_adapter.setCcInfos(item1_cc);
			item_adapter.notifyDataSetChanged();
			infoad.dismiss();
		};

	};

}
